package com.googlecode.jpocketmod;

import com.googlecode.jpocketmod.templates.pages.PageManager;
import com.googlecode.jpocketmod.templates.pages.PocketModPage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import layout.SpringUtilities;

/**
 *
 * @author David Sosby
 */
public class CreatePageDialog extends JDialog  {
	private Frame owner;
	
	JTextField nameField;
	JTextArea descriptionField;
	JComboBox categoryField;
	FileField bgImageField;
	
	public CreatePageDialog(Frame owner)  {
		super(owner);
		this.owner = owner;
		
		JPanel mainPanel = new JPanel(new GridLayout(1, 2));
		
		JPanel drawingPanel = new JPanel();
		drawingPanel.setBackground(Color.WHITE);
		drawingPanel.add(new JLabel("Drawing goes here"));
		drawingPanel.setPreferredSize(new Dimension(230, 350));
		
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
		
		JPanel pageOptions = new JPanel();
		pageOptions.setLayout(new SpringLayout());
		
		JLabel nameLabel = new JLabel("Name: ", JLabel.TRAILING);
		nameField = new JTextField(25);
		nameLabel.setLabelFor(nameField);
		
		JLabel bgImageLabel = new JLabel("BG Image: ", JLabel.TRAILING);
		bgImageField = new FileField();
		bgImageLabel.setLabelFor(bgImageField);
		
		JLabel categoryLabel = new JLabel("Category: ", JLabel.TRAILING);
		categoryField = new JComboBox(PageManager.getInstance().getAvailableCategories().toArray(new String[] {}));
		categoryField.setEditable(true);
		categoryLabel.setLabelFor(categoryField);
		
		JLabel descriptionLabel = new JLabel("Description: ", JLabel.TRAILING);
		descriptionField = new JTextArea(3, 25);
		descriptionLabel.setLabelFor(descriptionField);
		
		pageOptions.add(nameLabel);
		pageOptions.add(nameField);
		pageOptions.add(bgImageLabel);
		pageOptions.add(bgImageField);
		pageOptions.add(categoryLabel);
		pageOptions.add(categoryField);
		pageOptions.add(descriptionLabel);
		pageOptions.add(new JScrollPane(descriptionField));
		SpringUtilities.makeCompactGrid(pageOptions,
										4,		//NOTE: Change this option to number of rows
										2,
										6, 6,
										6, 6);
		
		
		JPanel dynamicContentOptions = new JPanel();
		dynamicContentOptions.add(new JLabel("Dynamic Content Options"));
		
		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e)  {
				PocketModPage newPage = new PocketModPage(nameField.getText());
				newPage.setCategory((String) categoryField.getSelectedItem());
				newPage.setDescription(descriptionField.getText());
				
				//TODO: Copy this image to the pagesFolder directory and store only the name (not the path)
				newPage.setBgImgFilepath(bgImageField.getValue());

				try  {
					PageManager.getInstance().addNewPage(newPage);
					
					setVisible(false);
				} catch (Exception ex)  {
					//TODO: Display dialog with error message
					System.err.println("Error occurred adding new page: " + ex);
				}
			} 
		});
		
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener()  {
			public void actionPerformed(ActionEvent e)  {
				setVisible(false);
			}
		});
		
		JPanel saveButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		saveButtons.add(cancelButton);
		saveButtons.add(saveButton);
		
		rightPanel.add(pageOptions);
		rightPanel.add(dynamicContentOptions);
		rightPanel.add(saveButtons);
		
		mainPanel.add(drawingPanel);
		mainPanel.add(rightPanel);
		
		this.setContentPane(mainPanel);
		this.pack();
		
		//TODO: Set resizeable and default button
	}
	
	@Override
	public void setVisible(boolean isVisible)  {
		if ( isVisible ) centerDialogInOwner();
		super.setVisible(isVisible);
	}
	
	private void centerDialogInOwner()  {
		if ( owner != null )  {
			Dimension screenSize = owner.getSize();							//Center the window
			this.setLocation( (int)owner.getLocation().getX() + (screenSize.width/2 - (this.getWidth()/2)),
					  (int)owner.getLocation().getY() + (screenSize.height/2 - (this.getHeight()/2)) );
		}
    }
}

class FileField extends JPanel  {
	private JTextField field = new JTextField();
	private JButton browseButton;
	
	public FileField()  {
		super();
		
		this.setLayout(new BorderLayout());
		
		browseButton = new JButton("...");
		
		//TODO: Finish this layout
		
		this.add(field, BorderLayout.CENTER);
		this.add(browseButton, BorderLayout.EAST);
	}
	
	public String getValue()  {
		return field.getText();
	}
}
